package view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import controller.Maquina;
import controller.exception.MaquinaInstantiationException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MenuJugar extends javax.swing.JPanel  {

	
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JTextField jTextField1;
	private JButton jButton2;
	private JButton jButton1;
	private JLabel indicadorCredito;
	private JLabel jLabel4;
	private JButton jButton3;
	private static MenuJugar instancia;
	

	private MenuJugar() throws MaquinaInstantiationException {
		super();
		initGUI();
	}
	
	public static MenuJugar getInstancia() throws MaquinaInstantiationException
	{
		if (instancia == null)
			instancia = new MenuJugar();
		return instancia;
	}
	
	private void initGUI() {
		try {
			{
				this.setLayout(null);
				this.setPreferredSize(new java.awt.Dimension(398, 211));
				{
					jLabel1 = new JLabel();
					this.add(jLabel1);
					jLabel1.setText("Que desea hacer?");
					jLabel1.setBounds(12, 14, 140, 16);
				}
				{
					jLabel2 = new JLabel();
					this.add(jLabel2);
					jLabel2.setText("Ingrese credito:");
					jLabel2.setBounds(12, 42, 102, 45);
				}
				{
					jLabel3 = new JLabel();
					this.add(jLabel3);
					jLabel3.setBounds(12, 161, 257, 50);
				}
				{
					jButton1 = new JButton();
					this.add(jButton1);
					jButton1.setText("Aumentar credito");
					jButton1.setBounds(237, 44, 131, 41);
					jButton1.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt)
						{
							try {
									Float credito=Float.valueOf(jTextField1.getText());
									Maquina.getInstancia().actualizarCredito(credito);

							} catch (MaquinaInstantiationException e) {
								System.out.println("La maquina ya fue creada");
							} catch (NumberFormatException e) {
								System.out.println("Se debe ingresar numero");
							}
							actualizarCredito();
						}

						
					});
				}
				{
					jButton2 = new JButton();
					this.add(jButton2);
					jButton2.setText("Formatear maquina");
					jButton2.setBounds(12, 149, 356, 39);
					jButton2.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt)
						{
							try {
									Maquina.getInstancia().formatearMaquina();
							} catch (MaquinaInstantiationException e) {
								System.out.println("La maquina ya fue creada");
							} catch (NumberFormatException e) {
								System.out.println("Se debe ingresar numero");
							}
						}

						
					});
				}
				{
					jTextField1 = new JTextField();
					this.add(jTextField1);
					jTextField1.setText("");
					jTextField1.setBounds(114, 44, 111, 41);
				}
				{
					jButton3 = new JButton();
					this.add(jButton3);
					this.add(getJLabel4());
					this.add(getIndicadorCredito());
					jButton3.setText("Jugar!");
					jButton3.setBounds(12, 97, 356, 41);
					jButton3.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							System.out.println("jugar.actionPerformed, event=");
							JFrame frame = new JFrame();
							frame.getContentPane().add(Juego.getInstance());
							frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
							frame.pack();
							frame.setVisible(true);
						}
					});
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel getJLabel4() {
		if(jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("Credito: $");
			jLabel4.setBounds(129, 15, 63, 15);
		}
		return jLabel4;
	}
	
	private JLabel getIndicadorCredito() {
		if(indicadorCredito == null) {
			indicadorCredito = new JLabel();
			indicadorCredito.setBounds(192, 17, 101, 10);
			indicadorCredito.setText("0");
		}
		actualizarCredito();
		return indicadorCredito;
	}
	
	private void actualizarCredito() {
		try {
			indicadorCredito.setText(Float.toString(Maquina.getInstancia().getCredito()));
		} catch (Exception e) {
			System.out.println("Imposible obtener credito.");
		}
	}

}
